var config = require('../../../utils/config.js');
var util = require('../../../utils/util.js');
var upload = require('../../../utils/upload.js');
var API_URL = config.API_URL;
var app = getApp();
let action;
Page({
    data: {
        images: [
        ],
        images_url: [
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        action = options.action;
        let that = this;
        if (action == 'edit') {
            let demand = app.globalData.demand
            that.setData({
                images: demand.images,
                images_url: demand.images,
                max: demand.max_price,
                min: demand.min_price,
                info: demand.content
            })
        }
    },
    // 图片预览
    previewImage: function (e) {
        console.log(e);
        let index = e.currentTarget.dataset.index;
        let images = this.data.images;
        my.previewImage({
            current: index, // 当前显示图片的位置
            urls: images
        })
    },
    // 删除图片
    del_img: function (e) {

        let index = e.currentTarget.dataset.index;
        let images = this.data.images;
        // let images_url = this.data.images_url;
        // console.log(images_url)
        console.log(images[index])
        // let patt = new RegExp("myfile://");
        // console.log(patt.test(images[index]))
        // if (!patt.test(images[index])) {
        //     images_url.splice(index, 1);
        // }
        images.splice(index, 1);
        this.setData({
            images: images
            // images_url: images_url
        })
    },
    // 选择图片
    chooseImage: function (e) {
        let that = this;
        let images = that.data.images;
        let count = 9 - images.length;
        my.chooseImage({
            count: count, // 最多可以选择的图片张数
            success: function (res) {
                console.log(res)
                images = images.concat(res.apFilePaths[0]);
                that.setData({
                    images: images
                })
            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
    },
    save: function (e) {
        console.log(e);
        let that = this;
        let min = e.detail.value.min_price == '' ? '' : Number(e.detail.value.min_price);
        let max = e.detail.value.max_price == '' ? '' : Number(e.detail.value.max_price);
        let info = e.detail.value.info;
        if (info == "") {
            util.showNotice("请输入求购描述")
            return
        }
        if (min == "") {
            if (max != "") {
                min = 0;
            }
        } else if (max == "") {
            if (min != "") {
                util.showNotice("请输入最高价");
                return
            }
        } else if (min > max) {
            util.showNotice("最低价不能大于最高价");
            return
        }
        my.showLoading({
            content: '上传图片中',
        })
        let url;
        if (action == 'add') {
            url = API_URL.demand_add;
        } else {
            url = API_URL.demand_edit;
        }
        var emptyArray = new Array();
        var patt = new RegExp("myfile://");
        console.log(that.data.images)
        // 这里是拿到上传图片的数组，实际上就是that.data.image，用了reduce把上传的图片放进数组
        var filePaths = that.data.images.reduce((x, y) => {
            var z;
            if (patt.test(y)) {
                z = x.concat(y);
               

            } else {
                z = x;

            }
            console.log(z)
            return z;
        }, emptyArray);
        console.log(filePaths)
        var img_url = that.data.images_url;
        upload.upload(API_URL.upload_img, filePaths, 0, 0, 0, filePaths.length, img_url, function (images_url) {
            // console.log(images_url);
            my.hideLoading();
            let images_url_str = images_url.join('|');
            let demandId = app.globalData.demand ? app.globalData.demand.demandId : '';
            let params = {
                demand_id: demandId,
                min_price: min,
                max_price: max,
                desc: info,
                images: images_url_str
            }
            util.PRL(url, params)
                .then(res => {
                    console.log(res);
                    my.showToast({
                        content: '保存成功',
                        duration: 800
                    })
                    app.globalData.refresh = true;
                    setTimeout(() => {
                        my.navigateBack({
                            delta: 1
                        })
                    }, 800)
                }).catch(res => {
                    console.log(res);
                })
        })
    }
})